package com.xsoft.demo12.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

import androidx.annotation.Nullable;

public class UserDataBaseHelper extends SQLiteOpenHelper {

    public UserDataBaseHelper(@Nullable Context context) {
        this(context, "student_db", null, 1);
    }

    public UserDataBaseHelper(@Nullable Context context, @Nullable String name, @Nullable SQLiteDatabase.CursorFactory factory, int version) {
        super(context, name, factory, version);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // 创建数据库1张表
        // 通过execSQL（）执行SQL语句（此处创建了1个名为student的表）
        String sql = "create table student(id integer primary key autoincrement,name varchar(64),phone_number varchar(11),password varchar(64))";
        db.execSQL(sql);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    }

    /**
     * 插入注册用户
     *
     * @param name
     * @param phoneNumber
     * @param password
     */
    public void insert(String name, String phoneNumber, String password) {
        SQLiteDatabase database = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("name", name);
        values.put("phone_number", phoneNumber);
        values.put("password", password);
        database.insert("student", null, values);
        database.close();
    }

    /**
     * 更新密码
     *
     * @param phoneNumber
     * @param password
     */
    public void update(String phoneNumber, String password) {
        SQLiteDatabase database = getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put("password", password);
        database.update("student", values, "phone_number = ?", new String[]{phoneNumber});
        database.close();
    }

    /**
     * 登录校验用户名和密码
     *
     * @param phoneNumber
     * @param password
     * @return
     */
    public boolean loginAccount(String phoneNumber, String password) {
        boolean result = false;
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.query("student", null,
                "phone_number = ? and password = ?",
                new String[]{phoneNumber, password}, null, null, null);
        while (cursor.moveToNext()) {
            result = true;
            break;
        }
        cursor.close();
        database.close();
        return result;
    }

    public String getName(String phoneNumber) {
        String name = null;
        SQLiteDatabase database = getWritableDatabase();
        Cursor cursor = database.query("student", null,
                "phone_number = ?",
                new String[]{phoneNumber}, null, null, null);
        if (cursor.moveToNext()) {
            name = cursor.getString(cursor.getColumnIndex("name"));
        }
        cursor.close();
        return name;
    }
}
